package com.wlr.blog.admin.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wlr.blog.util.common.aspect.LogTrack;
import com.wlr.blog.util.common.dto.Result;
import com.wlr.blog.web.common.entity.Category;
import com.wlr.blog.web.common.mapper.CategoryMapper;
import com.wlr.blog.web.common.service.CategoryService;
import com.wlr.blog.web.common.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lrwang
 * @version 1.0
 * @date 2022/11/20 10:00
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @GetMapping("/list")
    @LogTrack
    public Result<?> getCategoryList(Category category) {
        PageResult pageList = categoryService.getPageList(category);
        return Result.success(pageList);
    }

    @PutMapping("/update")
    @LogTrack
    public Result<?> updateCategory(@RequestBody Category category) {
        boolean update = categoryService.update(new UpdateWrapper<>(category));
        return update ? Result.success() : Result.error("更新失败");
    }

    @PostMapping("/save")
    @LogTrack
    public Result<?> saveCategory(@RequestBody Category category) {
        boolean save = categoryService.save(category);
        return save ? Result.success() : Result.error("保存失败");
    }

    @DeleteMapping("/del/{id}")
    @LogTrack
    public Result<?> delCategory(@PathVariable Integer id) {
        boolean b = categoryService.removeById(id);
        return b ? Result.success() : Result.error("删除失败");
    }
}
